package cn.lee.share.http;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import cn.lee.share.utils.GetIP;
import cn.lee.share.utils.LogUtils;

public class HttpServer implements Runnable{
    LogUtils log = LogUtils.getInstance();
    
    //shutdown command
    public static final String SHUTDOWN_COMMAND = "/SHUTDOWN";
    
    //the shutdown command received
    private boolean shutdown = false;
    ServerSocket serverSocket = null;
    
    public static void main(String[] args) {
        HttpServer server = new HttpServer();
        server.await();
    }
    
    public void await() {
        int port = 8080;
        log.log("create ServerSocket");
        try {
            serverSocket = new ServerSocket(port,0,null);
//            String ip = GetIP.wifiIP();
            String ip = "192.168.0.115";
//            serverSocket = new ServerSocket(port, 1, InetAddress.getByName(ip));
            log.log("server is bound? " + serverSocket.isBound());
            log.log(serverSocket.getInetAddress().toString() + " " +serverSocket.getLocalPort());
        } catch (IOException e) {
            log.log("create ServerSocket error" +e.getMessage());
            e.printStackTrace();
            System.exit(1);
        }
        log.log("create ServerSocket finished");
        
        //loop waiting for a request
        while(!shutdown){
            Socket socket = null;
            InputStream in = null;
            OutputStream output = null;
            try {
                log.log("wait.. for new socket");
                socket = serverSocket.accept();
                log.log("create a new socket");
                if(shutdown){
                    log.log("server is shut down");
                    break;
                }
                in = socket.getInputStream();
                log.log("get inputStream");
                output = socket.getOutputStream();
                log.log("get outPutStream");
                //create request object and parse
                Request request = new Request(in);
                request.parse();
                //create Response object
                Response response = new Response(output);
                response.setRequest(request);
                
                    DynamicProcesser DynamicProcesser = new DynamicProcesser();
                    DynamicProcesser.process(request, response);
                
                //close the socket
                socket.close();
                
                //check if the previous URI is a shutdown command
//                shutdown = SHUTDOWN_COMMAND.equals( request.getUri() );
            }catch (Exception e) {
                log.log("error " + e.getMessage());
                e.printStackTrace();
                continue;
            }
        }
    }
    public void shutDown(){
        log.log("before shutDown");
        try {
            serverSocket.close();
            log.log("after shutDown");
        } catch (IOException e) {
            log.log(" shutDown error "+e.getMessage());
            e.printStackTrace();
        }
        shutdown = true;
    }

    public void run() {
        await();
    }
}
